package com.chao.common.util;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.NumberUtil;

public class ObjectCheckUtil {

    //特殊字符
    private static String[] sqlKeywords = {"exec", "execute", "insert", "select", "delete", "update", "alter", "into", "substr", "ascii",
            "drop", "*", "chr", "mid", "master", "truncate", "char", "declare", "sitename", "net user", "xp_cmdshell", ";", "-", "+",
            "create", "table", "from", "grant", "use", "group_concat", "column_name", "information_schema.columns", "table_schema",
            "union", "where", "order", "by", "count", "--", "//", "/", "#", "or 1 = 1", "or '", "' or", "or'", "'or", ")or", ") or", "or(",
            "or (", " or", "or ", " and", "and ", ")and", "and(", ",and", "and'"};

    /**
     * 是否没有特殊字符
     *
     * @param value
     * @return
     */
    public static boolean checkRuleWithNoSpecial(Object value) {
        try {
            if (value == null) {
                return false;
            }
            String strValue = value.toString().toLowerCase();//统一转为小写
            for (int i = 0; i < sqlKeywords.length; i++) {
                if (strValue.indexOf(sqlKeywords[i]) >= 0) {
                    return false;
                }
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 是否是身份证号
     *
     * @param value
     * @return
     */
    public static boolean checkRuleWithIsIdcard(Object value) {
        try {
            if (value == null) {
                return false;
            }
            String strValue = value.toString();
            return IdcardUtil.isValidCard(strValue);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 是否是数字
     *
     * @param value
     * @return
     */
    public static boolean checkRuleWithIsNumber(Object value) {
        try {
            if (value == null) {
                return false;
            }
            String strValue = value.toString();
            return NumberUtil.isNumber(strValue);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 是否是手机号
     *
     * @param value
     * @return
     */
    public static boolean checkRuleWithIsMobile(Object value) {
        try {
            if (value == null) {
                return false;
            }
            String strValue = value.toString();
            return Validator.isMobile(strValue);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
